﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Dependencies;
using Autofac;
using Autofac.Builder;
using Autofac.Core;
using Autofac.Integration.WebApi;


namespace AutoFacWrapper
{
    public static class ClassFactory
    {
        public static Func<string, Type, object, object> Getinstance;
        private static readonly Dictionary<string, ILifetimeScope> dicILifetimeScopes;

        static ClassFactory()
        {
            IConainer = null;
            Builder = new ContainerBuilder();
            dicILifetimeScopes = new Dictionary<string, ILifetimeScope>();
        }

        /// <summary>
        ///     readOnly
        /// </summary>
        public static ContainerBuilder Builder { get; private set; }

        /// <summary>
        ///     readOnly
        /// </summary>
        public static IContainer IConainer { get; private set; }

        /// <summary>
        ///     IConainer 相当于单例
        /// </summary>
        /// <returns></returns>
        public static IContainer GetContainer()
        {
            if (IConainer == null)
            {
                IConainer = Builder.Build(ContainerBuildOptions.None);
            }
            return IConainer;
        }

        /// <summary>
        ///     获取实例 泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T GetInstance<T>(params Parameter[] parameters)
        {
            if (parameters.Length == 0)
            {
                return GetContainer().Resolve<T>();
            }
            return GetContainer().Resolve<T>(parameters);
        }

        /// <summary>
        ///     重写
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T GetInstance<T>()
        {
            return GetContainer().Resolve<T>();
        }

        public static object GetInstance(Type type, params Parameter[] parameters)
        {
            if (parameters.Length == 0)
            {
                return GetContainer().Resolve(type);
            }

            return GetContainer().Resolve(type, parameters);
        }

        public static object GetInstance(Type type)
        {
            return GetInstance(type, new Parameter[0]);
        }

        /// <summary>
        ///     注册程序集
        /// </summary>
        /// <param name="parameters"></param>
        public static void RegisterAssemblis(params Assembly[] parameters)
        {
            //AsSelf 必须写，注册程序集里没有继承接口的Types
            //Below is the code for my Autofac module.  
            //Please make sure you add the .AsSelf convention.  
            //If you do NOT do this your hubs will not be created.  
            //This is because there is no interface attached so Autofac will have no idea how to create the class.
            //AsImplementedInterfaces 
            Builder.RegisterAssemblyTypes(parameters)
                .AsImplementedInterfaces().AsSelf();

        }

        /// <summary>
        ///     注册成单例
        /// </summary>
        /// <typeparam name="TLT"></typeparam>
        /// <typeparam name="T"></typeparam>
        public static void RegisterTypeSingleInstance<TLT, T>() where TLT : T
        {
            Builder.RegisterType<TLT>().As<T>().SingleInstance();
        }

        public static void RegisterTypePreLifeTime<TLT, T>() where TLT : T
        {
            Builder.RegisterType<TLT>().As<T>().InstancePerLifetimeScope();
        }

        /// <summary>
        ///     注册成单例
        /// </summary>
        /// <typeparam name="TLT"></typeparam>
        /// <typeparam name="T"></typeparam>
        public static void RegisterTypeSingleInstance<TLT>()
        {
            Builder.RegisterType<TLT>().SingleInstance();
        }

        /// <summary>
        ///     正常注册
        /// </summary>
        /// <typeparam name="TLT"></typeparam>
        /// <typeparam name="T"></typeparam>
        public static void RegisterTypePerDependency<TLT, T>() where TLT : T
        {
            Builder.RegisterType<TLT>().As<T>().InstancePerDependency();
        }

        /// <summary>
        ///     正常注册注册成单例
        /// </summary>
        /// <typeparam name="TLT"></typeparam>
        /// <typeparam name="T"></typeparam>
        public static void RegisterTypePerDependency<TLT>()
        {
            Builder.RegisterType<TLT>().InstancePerDependency();
        }

        public static void RegisterApiControllers(Assembly ssembly)
        {
            Builder.RegisterApiControllers(ssembly);
        }

        public static void RegisterDependencyResolver(IDependencyResolver dependencyResolver) 
        {
            dependencyResolver=new AutofacWebApiDependencyResolver(GetContainer());
        }

        
    }
}